package com.zx.viewlearn.views.mypullrorefresh;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import com.zx.viewlearn.views.pullableview.Pullable;

/**
 * Author     Sdz
 * Email      946616236@qq.com
 * Time       2016/11/11 0011
 * Function   可上拉下拉刷新的RecyclerView
 */

public class PullableRecyclerView extends RecyclerView implements Pullable {

    public static final int TYPE_LIN = 0;

    public static final int TYPE_GRID = 1;

    public static final int TYPE_STAG = 2;
    /**
     * RecyclerView的manager类型
     * 0：默认LinearLayoutManager
     * 1：网格布局GridLayoutManager
     * 2：瀑布布局StaggeredGridLayoutManager
     */
    private int managerType = 0;
    //是否可以上拉
    private boolean canPullUp = true;
    //是否可以下拉
    private boolean canPullDown = true;
    //列表最后项
    private int lastItemPosition;
    //列表可见项
    private int firstItemPosition;
    //布局管理
    private LayoutManager layoutManager;

    // 用来记录lastItem的position
    // 由于瀑布流有多个列 所以此处用数组存储
    int column;
    int positions[];

    public PullableRecyclerView(Context context) {
        super(context);
    }

    public PullableRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullableRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setCanPullDown(boolean canPullDown) {
        this.canPullDown = canPullDown;
    }

    public void setCanPullUp(boolean canPullUp) {
        this.canPullUp = canPullUp;
    }


    @Override
    public boolean canPullDown() {
        getManager();
        // 没有item的时候也可以下拉刷新
        if (getAdapter() == null && canPullDown) {
            return true;
        }
        return (getAdapter().getItemCount() == 0 || firstItemPosition == 0 && getChildAt(0).getTop() >= 0) && canPullDown;
    }

    @Override
    public boolean canPullUp() {
        getManager();
        if (getAdapter() == null && canPullUp) {
            return true;
        }
        if (getAdapter().getItemCount() == 0 && canPullUp) {
            // 没有item的时候也可以上拉加载
            return true;
        } else if (lastItemPosition == (getAdapter().getItemCount() - 1) && managerType != TYPE_STAG) {
            // 滑到底部了
            if (getChildAt(lastItemPosition - firstItemPosition) != null
                    && getChildAt(lastItemPosition - firstItemPosition).getBottom() <= getMeasuredHeight() && canPullUp)
                return true;
        } else if (managerType == TYPE_STAG) {
            int flag =0;
            for (int i = 0; i < positions.length; i++) {
                if (positions[i] >= (getAdapter().getItemCount() - column)
                        && getLayoutManager().findViewByPosition(positions[i]).getBottom() <= getHeight()) {
                    flag++;
//                    return true;
                }
            }
            if (flag==positions.length){//保证每列最后一个都已经显示
                return true;
            }
        }
        return false;
    }

    private void getManager() {
        if (layoutManager == null) {
            layoutManager = getLayoutManager();
        }
        //判断是当前layoutManager是否为LinearLayoutManager
        // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
        if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
            //获取最后一个可见view的位置
            lastItemPosition = linearManager.findLastVisibleItemPosition();
            //获取第一个可见view的位置
            firstItemPosition = linearManager.findFirstVisibleItemPosition();
            managerType = TYPE_LIN;
        } else if (layoutManager instanceof GridLayoutManager) {
            //网格布局GridLayoutManager
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            lastItemPosition = gridLayoutManager.findLastVisibleItemPosition();
            firstItemPosition = gridLayoutManager.findFirstVisibleItemPosition();
            managerType = TYPE_GRID;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            //瀑布布局StaggeredGridLayoutManager
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            // 用来记录lastItem的position
            // 由于瀑布流有多个列 所以此处用数组存储
            column = staggeredGridLayoutManager.getColumnCountForAccessibility(null, null);
            positions = new int[column];
            // 获取lastItem的positions
            staggeredGridLayoutManager.findLastVisibleItemPositions(positions);
            managerType = TYPE_STAG;
        }
    }
}
